Repository navigation
Conversation
Implement the CloudEvents JSON Batch format (application/cloudevents-batch+json) in the core package. - formats/json.py: extract shared _event_to_dict/_dict_to_event helpers and add BATCH_CONTENT_TYPE, write_batch, read_batch, get_batch_content_type - formats/base.py: extend the Format protocol with the batch methods - bindings/http.py: add to_batch/from_batch and to_batch_event/from_batch_event, and reject batch payloads in from_http with a clear error - tests: cover round-trip, empty batch, mixed 0.3/1.0 versions, data_base64, invalid-element abort, non-array body, and from_http batch rejection Signed-off-by: PlugaruT <plugaru.tudor@protonmail.com>
- Split batch methods (write_batch/read_batch/get_batch_content_type) out of the Format protocol into a separate, runtime-checkable BatchFormat protocol so non-batch formats need not implement batching and existing Format implementations keep working. - Rename HTTP convenience wrappers to_batch_event/from_batch_event to to_events_batch/from_events_batch (a batch of events, not a batch event). - from_http: guard batch detection behind isinstance(BatchFormat), compare content types case-insensitively on both sides, and report the received vs. expected content type in the error. - read_batch: include the actual JSON type in the non-array error, reject non-object elements, and wrap per-element failures with the element index for a clearer error. Signed-off-by: PlugaruT <plugaru.tudor@protonmail.com>
04fc5ed to
dc60518
Compare
| content_type = "" | ||
| for key, value in message.headers.items(): | ||
| if key.lower() == CONTENT_TYPE_HEADER: | ||
| content_type = value | ||
| break | ||
|
|
||
| received_content_type = content_type.split(";")[0].strip().lower() |
There was a problem hiding this comment.
this content_type and received_content_type lines are only relevant if we're processing BatchFormat. Let's move them under the check or better have a private named method.
| paths cannot drift. | ||
| """ | ||
| event_data = event.get_data() | ||
| event_dict: dict[str, Any] = dict(event.get_attributes()) |
There was a problem hiding this comment.
| event_dict: dict[str, Any] = dict(event.get_attributes()) | |
| event_attributes: dict[str, Any] = dict(event.get_attributes()) |
| event_dict: dict[str, Any] = dict(event.get_attributes()) | ||
| specversion = event_dict.get("specversion", SPECVERSION_V1_0) | ||
|
|
||
| if event_data is not None: |
There was a problem hiding this comment.
Why don't we invert the condition? This way you'll have a fast way out with return event_dict and then just continue processing the branches. this will allow us to get rid of the nesting
There was a problem hiding this comment.
🟡 Changes recommended
Batch writers reject normally typed event lists due to invariant list annotations, and HTTP batch helpers lack required validation that all events share the same specversion.
2 open issues
What changed in this PR
Adds JSON Batch support to cloudevents.core and its HTTP binding, while keeping batching optional for other formats.
Changes:
- Adds shared JSON conversion helpers and batch serialization/deserialization.
- Introduces a separate
BatchFormatprotocol, HTTP batch helpers, and JSON-default wrappers. - Adds batch tests covering round trips, version detection, invalid input, and rejection by single-event HTTP parsing.
| File | Description |
|---|---|
tests/test_core/test_format/test_json.py |
Tests JSON batch encoding, decoding, and invalid elements. |
tests/test_core/test_bindings/test_http.py |
Tests HTTP batch helpers and single-event batch rejection. |
src/cloudevents/core/formats/json.py |
Implements JSON batch support using shared event conversion helpers. |
src/cloudevents/core/formats/base.py |
Defines the optional, runtime-checkable BatchFormat protocol. |
src/cloudevents/core/bindings/http.py |
Adds batch APIs and batch-content detection in from_http. |
Review effort: Balanced
| :return: HTTPMessage with the batch in the body | ||
| """ | ||
| headers = {CONTENT_TYPE_HEADER: event_format.get_batch_content_type()} | ||
| body = event_format.write_batch(events) |
| (e.g. ``from_http``) can detect batch capability with ``isinstance``. | ||
| """ | ||
|
|
||
| def write_batch(self, events: list[BaseCloudEvent]) -> bytes: |
|
@PlugaruT still interested in this? Otherwise we'll close it |
I am but no time on my end. I'll close and reopen later in case I come back to it |

What
Adds support for the CloudEvents JSON Batch format (
application/cloudevents-batch+json) to thecloudevents.corepackage, across the JSON format layer and the HTTP binding.Batch mode is only defined by the spec for the HTTP protocol binding (via the JSON Batch format); Kafka and AMQP define only structured/binary modes, so this change is intentionally scoped to JSON + HTTP.
Changes
formats/json.py: refactorwrite/readonto shared_event_to_dict/_dict_to_eventhelpers (no behavior change), then addBATCH_CONTENT_TYPE,write_batch,read_batch, andget_batch_content_type.formats/base.py: extend theFormatprotocol withwrite_batch/read_batch/get_batch_content_typeso any format implements the same contract.bindings/http.py: addto_batch/from_batchplus theto_batch_event/from_batch_eventJSON-default wrappers;from_httpnow rejects batch payloads with a clear, case-insensitive error pointing callers tofrom_batch.Behavior
data_base64for v1.0,datacontentencoding: base64for v0.3).ValueError.Tests
Added coverage for round-trip, empty batch, mixed-version batches,
data_base64elements, invalid-element abort, non-array body, andfrom_httpbatch rejection (incl. charset param and mixed-case content type). Fulltests/test_coresuite passes (356 tests);ruffclean.